import com.sun.xml.internal.ws.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.util.regex.Pattern;

public class StringUtil {
	/**
	 * 1.查询某个字符串第一次出现的位置
	 * 
	 * @param input
	 * @param query
	 */
	public static void getFirstMatchingIndex(String input, String query) {
		char[] inputChars = input.toCharArray();
		char[] queryChars = query.toCharArray();
		int inputLength = input.length();
		int queryLength = query.length();

		int inputIndex = 0;
		int queryIndex = 0;
		while (inputIndex < inputLength && queryIndex < queryLength) {
			if (inputChars[inputIndex] == queryChars[queryIndex]) {
				queryIndex++;
				inputIndex++;
			} else {
				inputIndex = inputIndex - queryIndex + 1;
				queryIndex = 0;
			}
		}

		int index = queryIndex == queryLength ? (queryLength > 1 ? inputIndex - queryLength : inputIndex - 1) : -1;
		System.out.println("first matching index:" + index);
	}

	/**
	 * 2.查询某个字符串最后一次出现的位置
	 * 
	 * @param input
	 * @param query
	 */
	public static void getLastMatchingIndex(String input, String query) {
		char[] inputChars = input.toCharArray();
		char[] queryChars = query.toCharArray();
		int inputLength = input.length();
		int queryLength = query.length();
		int inputIndex = inputLength - 1;
		int queryIndex = queryLength - 1;
		int matchingLenght = 0;
		while (inputIndex >= 0 && queryIndex >= 0) {
			if (inputChars[inputIndex] == queryChars[queryIndex]) {
				inputIndex--;
				queryIndex--;
				matchingLenght++;
			} else {
				inputIndex = matchingLenght <= 0 ? (inputIndex - 1) : inputIndex;
				queryIndex = queryLength - 1;
				matchingLenght = 0;
			}
			System.out.println("i:" + inputIndex + ",q:" + queryIndex);
		}
		System.out.println("last matching index:" + (matchingLenght == queryLength ? (inputIndex + 1) : -1));
	}

	// 3.判断是否是实数
	public static boolean isRealNumber(String str) {
		Pattern pattern = Pattern.compile("^(-)?[0-9]+(.[0-9]+)?$");
		return pattern.matcher(str).matches();
	}

	// 4.将primaryKey转化为时间类型的字符串
	public static String primaryKeyToDate(String primaryKey) {
		if (primaryKey.length() < 19) {
			return "";
		} else {
			// 字符串截取的规则都是左闭区间右开区间
			primaryKey = primaryKey.substring(0, 18);
			StringBuffer sb = new StringBuffer();
			primaryKey = sb.append(primaryKey).insert(10, " ").toString();
			return primaryKey;
		}

	}

	// 5.格式化时间类型,solr专用(将2016/05/03 09:31:02类型转化为2016-05-03T09:31:02Z)
	public static String formatDate(String date) {
		Date queryDate = null;
		SimpleDateFormat formatter = null;
		String result = null;
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			queryDate = df.parse(date);
			formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
			formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
			result = formatter.format(queryDate);
//			System.out.println("时间：" + result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
//		if (StringUtils.isEmpty(date) || date.length() < 19) {
//			return "";
//		} else {
//			// 字符串截取的规则都是左闭区间右开区间
//			date = date.substring(0, 19);
//			date = date.replaceAll("/", "-").replaceAll(" ", "T");
//
//			return date + "0Z";
//		}

	}

	/**
	 * 格式化时间类型将UTC格式的时间转换为北京时间
	 * 
	 * @Description:
	 * @author 高杨
	 * @date 2018年8月21日 上午9:07:13
	 * @return String
	 */
	public static String utc2BeiJingDate(String date) {
		if (StringUtil.isNullOrEmpty(date) || date.length() < 20) {
			return "";
		} else {
			// 去掉Z，替换-为/,替换T为空格
			return date.substring(0, 19).replaceAll("-", "/").replaceAll("T", " ");
		}

	}

	public static void main(String[] args) {

		System.out.println(isRealNumber("-21.0"));
		System.out.println(primaryKeyToDate("2017-11-1611:17:211"));
		System.out.println(formatDate("2016/05/03 09:31:02           "));

	}

	public static boolean isNullOrEmpty(String str) {
		return str == null || str.trim().isEmpty();
	}
}
